﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class Holidays : Form
    {
        public Holidays()
        {
            InitializeComponent();
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            labelDay.Text = monthCalendar1.SelectionRange.Start.ToLongDateString();
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            
        }

        private void Holidays_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable holidayTable = HolidayServices.GetHolidayCollectionDataTable();
                dataGridView.DataSource = holidayTable;

                HolidayCollection collection = HolidayServices.GetHolidayCollection();

                for (int iI = 0; iI < collection.Count; iI++)
                {
                    monthCalendar1.AddBoldedDate(collection[iI].day);
                }

                monthCalendar1.UpdateBoldedDates();

                dataGridView.Width = Size.Width - 40;
                dataGridView.Height = Size.Height - 140;
            }
            catch (System.Exception ex)
            {

            }

            WindowState = FormWindowState.Maximized;
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void Holidays_SizeChanged(object sender, EventArgs e)
        {
            dataGridView.Height = Size.Height - 160;
            groupBox1.Left = Width - 550;
            dataGridView.Width = Size.Width - 600;
            try
            {
                dataGridView.Columns[0].Width = ((dataGridView.Width - 2) * 40) / 100;
                dataGridView.Columns[1].Width = ((dataGridView.Width - 2) * 60) / 100;
            }
            catch (System.Exception ex)
            {
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            Holiday holiday = new Holiday();
            holiday.day = monthCalendar1.SelectionRange.Start;
            holiday.description = textBoxDescription.Text;

            try
            {
                HolidayServices.Add(holiday);
            }
            catch (System.Exception ex)
            {
                if (ex.Message.StartsWith("ORA-00001: unique"))
                {
                    MessageBox.Show("Holiday " + holiday.day.ToLongDateString() + " already added", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Failed to add new holiday\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }
            MessageBox.Show("Holiday added successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            monthCalendar1.AddBoldedDate(monthCalendar1.SelectionRange.Start);
            monthCalendar1.UpdateBoldedDates();

            try
            {
                DataTable holidayTable = HolidayServices.GetHolidayCollectionDataTable();
                dataGridView.DataSource = holidayTable;
                dataGridView.Refresh();
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private bool validateInputs()
        {
            if (labelDay.Text == "")
            {
                MessageBox.Show("Select a date from calendar", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (textBoxDescription.Text == "")
            {
                MessageBox.Show("Enter holiday description", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 0)
            {
                MessageBox.Show("No record selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            EditHoliday editHolidayDlg = new EditHoliday((DateTime)dataGridView.SelectedRows[0].Cells[0].Value, dataGridView.SelectedRows[0].Cells[1].Value.ToString());
            editHolidayDlg.ShowDialog();

            try
            {
                DataTable holidayTable = HolidayServices.GetHolidayCollectionDataTable();
                dataGridView.DataSource = holidayTable;
                dataGridView.Refresh();
            }
            catch (System.Exception ex)
            {
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 0)
            {
                MessageBox.Show("No record selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DateTime day = (DateTime)dataGridView.SelectedRows[0].Cells[0].Value;

            Holiday holiday = null;
            try
            {
                holiday = HolidayServices.GetHolidayByDay(day);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid holiday", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (holiday == null)
            {
                MessageBox.Show("Invalid holiday", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DialogResult result = MessageBox.Show("Do you really want to delete holiday " + holiday.day.ToLongDateString() + "?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (result != DialogResult.Yes)
            {
                return;
            }

            try
            {
                HolidayServices.Delete(holiday.day);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to delete holiday\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            dataGridView.Rows.RemoveAt(dataGridView.SelectedRows[0].Index);

            try
            {
                HolidayCollection collection = HolidayServices.GetHolidayCollection();

                monthCalendar1.RemoveAllBoldedDates();

                for (int iI = 0; iI < collection.Count; iI++)
                {
                    monthCalendar1.AddBoldedDate(collection[iI].day);
                }

                monthCalendar1.UpdateBoldedDates();
            }
            catch (System.Exception ex)
            {

            }

            MessageBox.Show("Holiday deleted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
